<?php

namespace app\api\controller;

use app\api\model\Account;

class Base
{
    //注册功能
    public function register()
    {
        //传入学号，姓名，密码注册
        $no = input('get.no');
        $name = input('get.name');
        $password = input('get.password');
        if (empty($no) || empty($name) || empty($password)) {
            output_error('请您填写完整信息');
        }
        //规则1：学号唯一
        $data = (new \app\api\model\account)->where('member_id', '=', $no)->find();
        if (!empty($data)) output_error('学号已被注册');

        //规则2：密码md5加密
        $data = array();
        $data['member_id'] = $no;
        $data['member_name'] = $name;
        $data['member_password'] = md5($password);
        $data['member_token'] = '1';
        $data['member_gender'] = '1';
        $data['add_time'] = time();
        (new \app\api\model\account)->insert($data);
        output_data('1');
    }
    //找回密码
    //登录功能
    public function login()
    {
        //通过学号，密码登录，返回Token
        $no = input('get.no');
        $password = input('get.password');
        if (empty($no) || empty($password)) {
            output_error('请您填写完整信息');
        }
        $condition = array();
        $condition[] = ['member_no', '=', $no];
        $condition[] = ['member_password', '=', md5($password)];
        $data = (new \app\api\model\account)->where($condition)->find();
        if (empty($data)) output_error('您的学号与密码不匹配');
        //规则1：token由学号+当前时间戳md5加密生成
        $token=md5($no.time());
        (new \app\api\model\account)->Where('member_id','=',$data['member_id'])->update(array('member_token'=>$token));
        output_data($token);
    }
}
